<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    1.trim 实现字符串首尾去掉空格
    <script>
        let str="     happy travel   ";
        console.log(''+str+'');
        console.log(str.trim());
    </script>

    <!-- 封装函数  -->
    <!-- 将字符串的首尾空格替换成空隙 -->
    <script>
         let str1="     happy travel   ";
         let str2="     happy travel   ";
         function ctrim(str){
            if(!typeof str==="string") return;
            return str.replace(/^\s+|\s+$/g,"")
         }
         console.log("("+ctrim(str1)+")");
         console.log("("+ctrim(str2)+")");
    </script>

    <!-- 3.replace的第二个参数 函数作为参数,函数的返回值作为替换的结果
    将字符串中的数字换成** -->
    <script>
        // let str3="a123456543ccferfwdfwddddd9999";
        // let result=str3.replace(/\d+/g,function(word){
        //     return word="*"
        // })
        // console.log(result);

        let str4="a123456543ccferfwdfwddddd9999";
        let result=str4.replace(/\d+/g,"")
        console.log(result);
    </script>
</body>
</html>